﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Collections.Generic;

using Marking.Dashboard.ViewModels.Sales;
using Marking.Dashboard.Services.SalesProxies;
using Marking.Dashboard.ViewModels.Finance;
using Marking.Dashboard.Services.FinanceProxies;

namespace Marking.Dashboard.Services.Remotes
{
    public class RemoteSalesService : ISalesService
    {
        public void LoadClothSalesInfos(
            string cycleType,
            DateTime startDate,
            DateTime endDate,
            Action<IEnumerable<SalesInfo>> action)
        {
            var proxy = new SalesServiceClient();

            proxy.LoadSalesInfosCompleted += (send, e) =>
            {
                action(e.Result.Select(x => new SalesInfo
                {
                    CycleType = cycleType,
                    StartDate = x.StartDate,
                    EndDate = x.EndDate,
                    Quantity = x.Quantity
                }));
            };

            proxy.LoadSalesInfosAsync(cycleType, startDate, endDate);
        }

        public void LoadMoneySalesInfos(
            string cycleType,
            DateTime startDate,
            DateTime endDate,
            Action<IEnumerable<SalesInfo>> action)
        {
            var proxy = new FinanceServiceClient();

            proxy.LoadFinanceInfosCompleted += (send, e) =>
            {
                action(e.Result.Select(x => new SalesInfo
                {
                    CycleType = cycleType,
                    StartDate = x.StartDate,
                    EndDate = x.EndDate,
                    Quantity = x.TotalMoney,
                    ExtraInfos = new FinanceWindowModel
                    {
                        ReceivableInfos = new FinancePieChartViewModel("收款明细，合计（{0:N0}）元。")
                        {
                            new FinanceInfo { ItemName = "销售收款", Quantity = x.SK_XSMoney, StartDate = x.StartDate, EndDate = x.EndDate },
                            new FinanceInfo { ItemName = "合同收款", Quantity = x.SK_HTMoney, StartDate = x.StartDate, EndDate = x.EndDate },
                            new FinanceInfo { ItemName = "预收款", Quantity = x.SK_YuShouMoney, StartDate = x.StartDate, EndDate = x.EndDate },
                            new FinanceInfo { ItemName = "返点收款", Quantity = x.SK_FDMoney, StartDate = x.StartDate, EndDate = x.EndDate },
                            new FinanceInfo { ItemName = "其它收款", Quantity = x.SK_QTMoney, StartDate = x.StartDate, EndDate = x.EndDate }
                        },
                        PayableInfos = new FinancePieChartViewModel("退款明细，合计（{0:N0}）元。")
                        {
                            new FinanceInfo { ItemName = "退货退款", Quantity = x.TK_THMoney, StartDate = x.StartDate, EndDate = x.EndDate },
                            new FinanceInfo { ItemName = "返点退款", Quantity = x.TK_FDMoney, StartDate = x.StartDate, EndDate = x.EndDate },
                            new FinanceInfo { ItemName = "回扣退款", Quantity = x.TK_HKMoney, StartDate = x.StartDate, EndDate = x.EndDate },
                            new FinanceInfo { ItemName = "其它退款", Quantity = x.TK_QTMoney, StartDate = x.StartDate, EndDate = x.EndDate }
                        }
                    }
                }));
            };

            proxy.LoadFinanceInfosAsync(cycleType, startDate, endDate);
        }
    }
}
